set more off
use "C:\Users\Andy Baker\OneDrive - UCB-O365\Data\Brazil\BEPS 2014\BEPS 2014 Merged Data v3.dta", clear
xtset idtelefone wave
gen respond=1
fillin idtelefone wave
recode respond .=0

*Before reshaping, generate some variables
	*vote choice
recode vb1 1=1 2=2 3=3 4=4 5=5 6=6 7=7 8=15 11=11 12=. 13=17 14=17 .a=16 .b=. .=., gen(vote)
recode vb3 13/14=17
replace vote=vb3 if wave==7 
label define vote 1 Rousseff 2 Neves 3 Campos 4 Marina 5 "Everaldo Pereira" 6 "Eduardo Jorge" 7 "Luciana Genro" 11 "Levy Fidelix" 15 Other 17 Abstain 16 DK
label values vote vote

recode disc5a 1/8=1 9/10=0 .a=0 .b=0, gen(disc_family)
recode disc6a 1/8=1 10=0  .a=0 .b=0, gen(disc_friend)

*THRESHOLD (I supplement the feeling thermometer with the candidate traits stuff since former was only asked in first wave)
replace vb8c=vb8c_marina if wave>2
replace vb9c=vb9c_marina if wave>2

	*For Dilma voters
egen tempaeciomarina=rowmean(vb8b vb9b vb8c vb9c)
egen tempdilma=rowmean(vb8a vb9a)
gen dilma_therm=tempdilma-tempaeciomarina

	*For Aecio voters
egen tempdilmamarina=rowmean(vb8a vb9a vb8c vb9c)
egen tempaecio=rowmean(vb8b vb9b)
gen aecio_therm=tempaecio-tempdilmamarina

	*For Marina voters (Campos in wave 2)
egen tempdilmaaecio=rowmean(vb8a vb9a vb8b vb9b)
egen tempmarina=rowmean(vb8c vb9c)
gen marina_therm=tempmarina-tempdilmaaecio

drop temp*

replace ft2=ft2[_n-1] if wave==2
replace ft3=ft3[_n-1] if wave==2
replace ft5=ft5[_n-1] if wave==2
replace ft6=ft6[_n-1] if wave==2
replace ft7=ft7[_n-1] if wave==2
replace ft2=ft2[_n-2] if wave==3
replace ft3=ft3[_n-2] if wave==3
replace ft5=ft5[_n-2] if wave==3
replace ft6=ft6[_n-2] if wave==3
replace ft7=ft7[_n-2] if wave==3
replace ft2=ft2[_n-3] if wave==4
replace ft3=ft3[_n-3] if wave==4
replace ft5=ft5[_n-3] if wave==4
replace ft6=ft6[_n-3] if wave==4
replace ft7=ft7[_n-3] if wave==4
replace ft2=ft2[_n-4] if wave==5
replace ft3=ft3[_n-4] if wave==5
replace ft5=ft5[_n-4] if wave==5
replace ft6=ft6[_n-4] if wave==5
replace ft7=ft7[_n-4] if wave==5

	*For Dilma voters
egen tempaeciomarina=rowmean(ft2 ft5)
egen tempdilma=rowmean(ft3)
gen dilma_therm2=tempdilma-tempaeciomarina

	*For Aecio voters
egen tempdilmamarina=rowmean(ft3 ft5)
egen tempaecio=rowmean(ft2)
gen aecio_therm2=tempaecio-tempdilmamarina

	*For Marina voters
egen tempdilmaaecio=rowmean(ft3 ft2)
egen tempmarina=rowmean(ft5)
gen marina_therm2=tempmarina-tempdilmaaecio

egen threshold_dilma=rowmean(dilma_therm dilma_therm2)
gen dilma_threshold=(threshold_dilma+10)/20

egen threshold_aecio=rowmean(aecio_therm aecio_therm2)
gen aecio_threshold=(threshold_aecio+10)/20

egen threshold_marina=rowmean(marina_therm marina_therm2)
gen marina_threshold=(threshold_marina+10)/20

	*Party intensity
recode vb11 (1501=1) (else=0), gen(partyintensePT1)
replace partyintensePT1=.5 if vb13==1501
replace partyintensePT1=. if wave~=1

recode vb11 (1503=1) (else=0), gen(partyintensePSDB1)
replace partyintensePSDB1=.5 if vb13==1503
replace partyintensePSDB1=. if wave~=1

recode vb11 (1504=1) (else=0), gen(partyintensePSB1)
replace partyintensePSB1=.5 if vb13==1504
replace partyintensePSB1=. if wave~=1

recode vb11 (1501=0) (1502=1) (1503/1504=0) (1505/1510=1) (.a=1) (.b=1) (.c=1), gen(partyintense_baseline1)
replace partyintense_baseline=.5 if vb13==1502 
replace partyintense_baseline=.5 if vb13>1504 & vb13<1511
replace partyintense_baseline=0 if vb13==1501
replace partyintense_baseline=0 if vb13==1503
replace partyintense_baseline=0 if vb13==1504
replace partyintense_baseline=. if wave~=1

	*Econ Evaluations
recode soct1 (1=5) (2=4) (3=3) (4=2) (5=1), gen(socio_current)
recode soct2 (1=3) (2=2) (3=1), gen(socio_change)
recode idio1 (1=5) (2=4) (3=3) (4=2) (5=1), gen(self_current)

polychoricpca soct1  soct2 idio1, score(econ_evaltemp) nscore(1) 
impute econ_evaltemp soct1  soct2 idio1 if wave==1, gen(econ_eval)
replace econ_eval=. if wave~=1

		*issues: social issues not really working here
polychoricpca soc11 soc7 pp10 pp11 pp12 pp13 pp14, score(econ_issues) nscore(1)
impute econ_issues soc11 soc7 pp10 pp11 pp12 pp13 pp14 if wave==1, gen(econ_issues)
replace econ_issues=. if wave~=1

	*Pres approval
recode m1 (1=5) (2=4) (3=3) (4=2) (5=1), gen(dilma_approve)
replace dilma_approve=. if wave~=1

	*I added in ideo and ft because the set of pk items is severely truncated at the bottom. 43% didn't know any.
recode pk1 pk2 pk3 pk4 pk5 (.a=0) (.b=0) (2=0)
rename l1 ideo
recode ideo (0/10=1) (.a=0) (.b=0) (.=.), gen(ideo_respond1) 
recode ft1-ft7 (0/10=1) (.a=0) (.b=0) (.=.)
egen ft_know=anycount(ft1 ft2 ft3 ft4 ft5 ft6 ft7) if wave==1, values(1)
irt grm pk1 pk2 pk3 pk4 pk5 ideo_respond1 ft_know if wave==1 [pweight=weight_censo]
predict aware_irt if wave==1, latent ebmeans
egen awarez=std(aware_irt)

/*THIS IS TRICKY
In Wave 6 and Wave 7, respondents were asked about the first and second round presidential vote choices of their most recently named discussants, 
identified by the relationship. Because of the close timing of waves, discussants named in Wave 5 were not asked about in Waves 6 and 7. 
So, means that Wave 6 R’s were asked about wave 4 discussants if they participated in wave 4, and wave 2 or 3 discussants if they did not 
participate in wave 4.
*/
xtset idtelefone wave

replace disc5b2 =disc5b if wave==4
replace disc6b2 =disc6b if wave==4

replace disc5b2 = disc5b if wave==3 & f.respond==0
replace disc6b2 = disc6b if wave==3 & f.respond==0

replace disc5b2 = disc5b if wave==2 & f.respond==0 & f2.respond==0
replace disc6b2 = disc6b if wave==2 & f.respond==0 & f2.respond==0

gen presvotetemp=vote if wave==6 
replace presvotetemp=vote if disc5b2~=. & disc6b2~=. 
replace presvotetemp=vote if wave==1

recode wave 1=1 2/5=2 6=6 7=7, gen(wave2)
egen discpresvote1=max(disc5b2), by(idtel wave2) 
egen discpresvote2=max(disc6b2), by(idtel wave2) 
egen presvote=max(presvotetemp), by(idtel wave2) 

keep if wave==1 | wave==5 | wave==6 | wave==7

reshape long discpresvote, i(idtelefone wave) j(discid) 
sort idtelefone discid wave

gen newid=idtelefone*10+discid
recode wave 1=4
xtset newid wave

*recode discpresvote 1=1 2=2 3=. 4=4 5/7=. 8=. 13/14=17
*recode presvote 1=1 2=2 3=. 4=4 5/15=. 16/17=17
recode discpresvote 1=1 2=2 3=15 4=4 5/8=15 13/14=17
recode presvote 1=1 2=2 3=15 4=4 5/15=15 16/17=17


*Recoding others to missing because it creates a problem for some categories
*recode discpresvote 15=. 

label define presvotetemp 1 Dilma 2 Aecio 3 Campos 4 Marina 5 "Everaldo Pereira" 6 "Eduardo Jorge" 7 "Luciana Genro" 11 "Levy Fidelix" 15 Other 17 "Abstain"
label values presvote presvotetemp
label values discpresvote presvotetemp

constraint drop _all
constraint define 3 [Dilma]partyintensePSB==0
constraint define 4 [Marina]partyintensePT==0
constraint define 100 [1]17.discpresvote==0

*Quick analysis of second round vote
recode vb3 2=1 1=0 17=., gen(presvote_rnd2_dv)
eststo clear
*Table A.10
eststo: reg presvote_rnd2_dv ib1.discpresvote ib1.l.discpresvote l2.econ_eval l2.dilma_approve l2.partyintensePSDB1 l2.partyintense_baseline1 l2.econ_issues ib1.l.presvote ib1.l2.presvote   /// 
if (presvote==4| presvote==15) & wave==6 [pweight=weight_censo], cluster(idtelefone) 
cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
esttab est1 using Brazil2014dyadic2ndround.rtf, label b(3) replace se star(* 0.05 ) nogap onecell
eststo clear

tab presvote, gen(presvote)
tab discpresvote, gen(discpresvote)

reg presvote_rnd2_dv discpresvote2 discpresvote3 discpresvote4 discpresvote5 l.discpresvote2 l.discpresvote3 l.discpresvote4 l.discpresvote5 ///
l2.econ_eval l2.dilma_approve l2.partyintensePSDB1 l2.partyintense_baseline1 l2.econ_issues l.presvote2 l.presvote3 l.presvote4 l.presvote5 ///
l2.presvote2 l2.presvote3 l2.presvote4 l2.presvote5  /// 
if (presvote==4| presvote==15) & wave==6  [pweight=weight_censo], cluster(idtelefone)   

*Figure 4.7
	*effect of dilma versus aecio voter when mr is marina voter
margins, at(discpresvote2==0 discpresvote3==0 discpresvote4==0 l.discpresvote2==0 l.discpresvote3==0 l.discpresvote4==0 l.discpresvote5==0  ///
l.presvote2==0 l.presvote3==1 l.presvote4==0 l.presvote5==0 l2.presvote2==0 l2.presvote3==1 l2.presvote4==0 l2.presvote5==0) ///
at(discpresvote2==1 discpresvote3==0 discpresvote4==0 l.discpresvote2==1 l.discpresvote3==0 l.discpresvote4==0 l.discpresvote5==0  ///
l.presvote2==0 l.presvote3==1 l.presvote4==0 l.presvote5==0 l2.presvote2==0 l2.presvote3==1 l2.presvote4==0 l2.presvote5==0) atmeans contrast(atcontrast(r))

margins, at(discpresvote2==0 discpresvote3==0 discpresvote4==0 ///
l.presvote2==0 l.presvote3==1 l.presvote4==0 l.presvote5==0 l2.presvote2==0 l2.presvote3==1 l2.presvote4==0 l2.presvote5==0) ///
at(discpresvote2==1 discpresvote3==0 discpresvote4==0 ///
l.presvote2==0 l.presvote3==1 l.presvote4==0 l.presvote5==0 l2.presvote2==0 l2.presvote3==1 l2.presvote4==0 l2.presvote5==0) atmeans contrast(atcontrast(r))

	*Full Model
*Table A.8
eststo clear
eststo: mlogit presvote ib1.discpresvote ib1.l.discpresvote l2.econ_eval l2.dilma_approve l2.partyintensePSDB1 l2.partyintensePSB1 l2.partyintense_baseline1 l2.econ_issues ib1.l.presvote ib1.l2.presvote   /// 
if presvote<15 & wave==6 [pweight=weight_censo], cluster(idtelefone)  base(1) 

mat define eb= get(_b)
mat define eV=get(VCE)
mat B1=eb["y1","Aecio:1b.discpresvote".."Aecio:17.discpresvote"]
mat B2=eb["y1","Marina:1b.discpresvote".."Marina:17.discpresvote"]
mat V1a=eV["Aecio:1b.discpresvote","Aecio:1b.discpresvote"]
mat V2a=eV["Aecio:2.discpresvote","Aecio:2.discpresvote"]
mat V3a=eV["Aecio:4.discpresvote","Aecio:4.discpresvote"]
mat V4a=eV["Aecio:15.discpresvote","Aecio:15.discpresvote"]
mat V5a=eV["Aecio:17.discpresvote","Aecio:17.discpresvote"]
mat V1b=eV["Marina:1b.discpresvote","Marina:1b.discpresvote"]
mat V2b=eV["Marina:2.discpresvote","Marina:2.discpresvote"]
mat V3b=eV["Marina:4.discpresvote","Marina:4.discpresvote"]
mat V4b=eV["Marina:15.discpresvote","Marina:15.discpresvote"]
mat V5b=eV["Marina:17.discpresvote","Marina:17.discpresvote"]
mat B=[.,B1,.,B2]
mat V=[.,V1a,V2a,V3a,.,V5a,.,V1b,V2b,V3b,.,V5b]
mat BFull=[B]'
mat VFull=[V]'
svmat BFull 
svmat VFull 

gen n=_n*-1

gen upperFull=BFull+1.9645*(VFull^.5)
gen lowerFull=BFull-1.9645*(VFull^.5)

replace BFull=. if VFull==.

*Figure 4.5C
twoway (rcap upperFull lowerFull n if n>-13, color(black) horizontal ) (scatter n BFull if n>-13, mcolor(black)) , ///
xtitle("{bf:Multinomial Logit Coefficient}", size(medium)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("") ///
legend(off) xline(0, lcolor(black)) yline(-6.5, lcolor(black))  ///
ylab(-2 "Alter vote choice=Dilma" -3 "{bf:Alter vote choice=Aécio}" -4 "Alter vote choice=Marina" -5 "Alter vote choice=other" -6 "Alter vote choice=none" ///
-8 "Alter vote choice=Dilma" -9 "Alter vote choice=Aécio" -10 "{bf:Alter vote choice=Marina}" -11 "Alter vote choice=other" -12 "Alter vote choice=none", ///
angle(horizontal) notick )  yscale(range(-12 -1)) xscale(range(0 3)) ///
xlab(-1 -.5 0 .5 1 1.5 2 2.5 3 3.5 4 4.5 5.0) ytick(-6.5, tlength(42) tlcolor(black)) ytitle("{bf:Independent Variables}", size(medium)) text(-1 2.1 "{bf:Pr(Y{it:{sub:e}}=Aécio) {&frasl} Pr(Y{it:{sub:e}}=Dilma)}", box bcolor(white)) ///
text(-7 2.1 "{bf:Pr(Y{it:{sub:e}}=Marina) {&frasl} Pr(Y{it:{sub:e}}=Dilma)}", box bcolor(white))  note("{it:N}=566", span size(medsmall))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2014DyadicFull.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_5C.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_5C.pdf", as(pdf) replace 

*	Just to get predicted prob
*tab discpresvote, gen(discpresvote)
*tab presvote, gen(presvote)

mlogit presvote discpresvote2 discpresvote3 discpresvote4 discpresvote5 l.discpresvote2 l.discpresvote3 l.discpresvote4 l.discpresvote5 ///
l2.econ_eval l2.dilma_approve l2.partyintensePSDB1 l2.partyintensePSB1 l2.partyintense_baseline1 l2.econ_issues l.presvote2 l.presvote3 l.presvote4 l.presvote5 l2.presvote2 l2.presvote3 l2.presvote4 l2.presvote5 /// 
if presvote<15 & wave==6 [pweight=weight_censo], cluster(idtelefone)  base(1) 

	*Marina to Marina
margins, at(discpresvote2==0 discpresvote3==1 discpresvote4==0 discpresvote5==0 l.discpresvote2==0 l.discpresvote3==1 l.discpresvote4==0 l.discpresvote5==0 ///
) atmeans predict(outcome(1)) predict(outcome(2)) predict(outcome(4))
	*Marina to Aécio
margins, at(discpresvote2==1 discpresvote3==0 discpresvote4==0 discpresvote5==0 l.discpresvote2==0 l.discpresvote3==1 l.discpresvote4==0 l.discpresvote5==0 ///
) atmeans predict(outcome(1)) predict(outcome(2)) predict(outcome(4))

	*Marina to Dilma
margins, at(discpresvote2==0 discpresvote3==0 discpresvote4==0 discpresvote5==0 l.discpresvote2==0 l.discpresvote3==1 l.discpresvote4==0 l.discpresvote5==0 ///
) atmeans predict(outcome(1)) predict(outcome(2)) predict(outcome(4))

	*Dilma to Dilma
margins, at(discpresvote2==0 discpresvote3==0 discpresvote4==0 discpresvote5==0 l.discpresvote2==0 l.discpresvote3==0 l.discpresvote4==0 l.discpresvote5==0 ///
) atmeans predict(outcome(1)) predict(outcome(2)) predict(outcome(4))

	*Stable Discussants
*Table A.8
eststo: mlogit presvote ib1.discpresvote l2.econ_eval l2.dilma_approve l2.partyintensePSDB1 l2.partyintensePSB1 l2.partyintense_baseline1 l2.econ_issues ib1.l.presvote ib1.l2.presvote   ///
if presvote<15 & wave==6 & discpresvote==l.discpresvote [pweight=weight_censo], cluster(idtelefone)  base(1) 

mat define eb= get(_b)
mat define eV=get(VCE)
mat B1=eb["y1","Aecio:1b.discpresvote".."Aecio:17.discpresvote"]
mat B2=eb["y1","Marina:1b.discpresvote".."Marina:17.discpresvote"]
mat V1a=eV["Aecio:1b.discpresvote","Aecio:1b.discpresvote"]
mat V2a=eV["Aecio:2.discpresvote","Aecio:2.discpresvote"]
mat V3a=eV["Aecio:4.discpresvote","Aecio:4.discpresvote"]
mat V5a=eV["Aecio:17.discpresvote","Aecio:17.discpresvote"]
mat V1b=eV["Marina:1b.discpresvote","Marina:1b.discpresvote"]
mat V2b=eV["Marina:2.discpresvote","Marina:2.discpresvote"]
mat V3b=eV["Marina:4.discpresvote","Marina:4.discpresvote"]
mat V5b=eV["Marina:17.discpresvote","Marina:17.discpresvote"]
mat B=[.,B1,.,B2]
mat V=[.,V1a,V2a,V3a,V5a,.,V1b,V2b,V3b,V5b]
mat BStableDisc=[B]'
mat VStableDisc=[V]'
svmat BStableDisc 
svmat VStableDisc 

gen upperStableDisc=BStableDisc+1.9645*(VStableDisc^.5)
gen lowerStableDisc=BStableDisc-1.9645*(VStableDisc^.5)

*Figure 4.6C
twoway (rcap upperStableDisc lowerStableDisc n if n>-11, color(black) horizontal ) (scatter n BStableDisc if n>-11, mcolor(black)) , ///
xtitle("{bf:Multinomial Logit Coefficient}", size(medium)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("") ///
legend(off) xline(0, lcolor(black)) yline(-5.5, lcolor(black))  ///
ylab(-2 "Alter vote intention=Dilma"  -3 "{bf:Alter vote intention=Aécio}" -4 "Alter vote intention=Marina" -5 "Alter vote intention=none"  ///
-7 "Alter vote intention=Dilma" -8 "Alter vote intention=Aécio" -9 "{bf:Alter vote intention=Marina}" -10 "Alter vote intention=none", ///
angle(horizontal) notick )  yscale(range(-10 -1)) xscale(range(0 3)) ///
xlab(-1.5 -1 -.5 0 .5 1 1.5 2 2.5 3 3.5 4 4.5) ytick(-5.5, tlength(46) tlcolor(black)) ytitle("{bf:Independent Variables}", size(medium)) text(-1 1.75 "{bf:Pr(Y{it:{sub:e}}=Aécio) {&frasl} Pr(Y{it:{sub:e}}=Rousseff)}", box bcolor(white)) ///
text(-6 1.75 "{bf:Pr(Y{it:{sub:e}}=Marina) {&frasl} Pr(Y{it:{sub:e}}=Rousseff)}", box bcolor(white)) note("{it:N}=356", span size(medsmall))
xyz
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2014StableFull.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_6C.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_6C.pdf", as(pdf) replace 

mlogit presvote discpresvote2 discpresvote3 discpresvote4 discpresvote5 l.discpresvote2 l.discpresvote3 l.discpresvote4 l.discpresvote5 ///
l2.econ_eval l2.dilma_approve l2.partyintensePSDB1 l2.partyintensePSB1 l2.partyintense_baseline1 l2.econ_issues l.presvote2 l.presvote3 l.presvote4 l.presvote5 l2.presvote2 l2.presvote3 l2.presvote4 l2.presvote5 /// 
if presvote<15 & wave==6 & discpresvote==l.discpresvote [pweight=weight_censo], cluster(idtelefone)  base(1) 


	*Dilma to Dilma
margins, at(discpresvote2==0 discpresvote3==0 discpresvote4==0 discpresvote5==0 l.discpresvote2==0 l.discpresvote3==0 l.discpresvote4==0 l.discpresvote5==0 ///
) atmeans predict(outcome(1)) predict(outcome(2)) predict(outcome(4))


cd "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
esttab est1 est2 using Brazil2014dyadic.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Table 3.5: The Correlates of Vote Choice in Brazil's 2014 Presidential Election Campaign: Multinomial Logit Models on Dyad-Level Data) ///
modelwidth(6) nonumbers mtitles("Full Sample" "Stable Discussants") compress /// 
addnote("Entries are multinomial logit coefficients with robust standard errors (corrected for clustering on main respondent) in parentheses.")

	*Awareness stuff
eststo: mlogit presvote ib1.discpresvote##c.l2.awarez ib1.l.presvote ib1.l2.presvote if presvote<15 & wave==6 & discpresvote==l.discpresvote ///
 [pweight=weight_censo], cluster(idtelefone)  base(1)

eststo: mlogit presvote ib1.discpresvote ib1.l.presvote ib1.l2.presvote if l2.awarez<0 & presvote<15 & wave==6 & discpresvote==l.discpresvote ///
& presvote<4 [pweight=weight_censo], cluster(idtelefone)  base(1)

eststo: mlogit presvote ib1.discpresvote ib1.l.presvote ib1.l2.presvote if l2.awarez>0 & presvote<15 & wave==6 & discpresvote==l.discpresvote ///
& presvote<4 [pweight=weight_censo], cluster(idtelefone)  base(1)


recode presvote 2=1 1=0 else=., gen(dilma_neves)
recode presvote 4=1 1=0 else=., gen(dilma_marina)
recode discpresvote 15=.  

*Instrumental Vars
ivregress 2sls  dilma_neves l2.econ_eval l2.dilma_approve l2.partyintensePSDB1 l2.partyintensePSB1 l2.partyintense_baseline1 l2.econ_issues ib1.l.presvote ib1.l2.presvote  /// 
(ib1.discpresvote =ib1.l.discpresvote) if wave==6 [pweight=weight_censo] , cluster(idtelefone) 

ivregress 2sls dilma_marina l2.econ_eval l2.dilma_approve l2.partyintensePSDB1 l2.partyintensePSB1 l2.partyintense_baseline1 l2.econ_issues ib1.l.presvote ib1.l2.presvote  /// 
(ib1.discpresvote =ib1.l.discpresvote) if wave==6 [pweight=weight_censo], cluster(idtelefone)

*Friends only, for environmental confounding
eststo clear
	*Stable Discussants
eststo: mlogit presvote ib1.discpresvote ib1.l.discpresvote l2.econ_eval l2.dilma_approve l2.partyintensePSDB1 l2.partyintensePSB1 l2.partyintense_baseline1 l2.econ_issues ib1.l.presvote ib1.l2.presvote   /// 
if presvote<15 & discid==2 & wave==6 [pweight=weight_censo], base(1) 

eststo: mlogit presvote ib1.discpresvote ib1.l.discpresvote l2.econ_eval l2.dilma_approve l2.partyintensePSDB1 l2.partyintensePSB1 l2.partyintense_baseline1 l2.econ_issues ib1.l.presvote ib1.l2.presvote   /// 
if presvote<15 & discid==2 & discpresvote==l.discpresvote & wave==6 [pweight=weight_censo], base(1) 

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
esttab est1 est2 using Brazil2014dyadicFRIENDS.rtf, label b(3) replace se star(* 0.05 ) nogap onecell
eststo clear
